#include "window_common.h"
#include "window_menu_user_add.h"
#include "gkt_pr_sensor.h"

enum {
	WIDGET_ID_TITLE = 0,
	WIDGET_ID_HINT,
	WIDGET_ID_CANCEL_OK,
	WIDGET_ID_MAX
};

GKT_WINDOW_WIDGET_DESCS_DEFINE(initialization) = {
	[WIDGET_ID_TITLE] = GKT_WIDGET_LABEL_DESC( 
			GKT_WIDGET_F_ALIGN_MIDDLE | GKT_WIDGET_F_ALIGN_CENTER | GKT_WIDGET_F_TITLE_MARK,
			WINDOW_FONT_NORMAL_SIZE,
			WINDOW_TITLE_FG_COLOR, WINDOW_TITLE_BG_COLOR,
			0, 0,
			GKT_SCREEN_WIDTH, WINDOW_WIDGET_NORMAL_HEIGHT,
			WINDOW_TEXT_ID_system_init),
	[WIDGET_ID_HINT] = GKT_WIDGET_LABEL_DESC(
			GKT_WIDGET_F_ALIGN_MIDDLE | GKT_WIDGET_F_ALIGN_CENTER,
			WINDOW_FONT_NORMAL_SIZE,
			GKT_WIDGET_FG_COLOR_DEFAULT, GKT_WIDGET_BG_COLOR_DEFAULT,
			0, WINDOW_WIDGET_NORMAL_HEIGHT + (WINDOW_WIDGET_NORMAL_HEIGHT / 2), 
			GKT_SCREEN_WIDTH, WINDOW_WIDGET_NORMAL_HEIGHT,
			WINDOW_TEXT_ID_press_pound_add_admin),
	[WIDGET_ID_CANCEL_OK] = GKT_WIDGET_LABEL_DESC(
			GKT_WIDGET_F_ALIGN_MIDDLE | GKT_WIDGET_F_ALIGN_CENTER,
			WINDOW_FONT_NORMAL_SIZE,
			GKT_WIDGET_FG_COLOR_DEFAULT, GKT_WIDGET_BG_COLOR_DEFAULT,
			0, WINDOW_WIDGET_NORMAL_HEIGHT * 3, 
			GKT_SCREEN_WIDTH, WINDOW_WIDGET_NORMAL_HEIGHT,
			WINDOW_TEXT_ID_cancel_ok),
};

typedef struct _window_initialization_data {
	window_user_add_data_s 	add_data;
	uint32_t	combined_key_counter;
	uint32_t	combined_key_value;
	uint16_t 	set_key_parameter;
	uint16_t    set_rfid_standby_mode;
} window_initialization_data_s;
static window_initialization_data_s s_window_initialization_data;

static void window_initialization_reset_combined_key(void)
{
	s_window_initialization_data.combined_key_counter = 0;
	s_window_initialization_data.combined_key_value = 0;
}

static int window_initialization_event_handler(
					uint32_t major_type, uint32_t minor_type, 
					uint32_t id, uint32_t data)
{
	int exit_now;
	int retval = GKT_EVENT_HANDLE_CONTINUE;
	
	if (GKT_EVENT_WINDOW == major_type) {
		if (GKT_WINDOW_ACTIVE == minor_type) {
			window_initialization_reset_combined_key();
			window_common_mask_except_keys(~0);
			gkt_window_set_voice(WINDOW_VOICE_COMBINED_ID(please_press_pound_to_add_admin_user), 0);
		}
	}
	else if (GKT_EVENT_KEY == major_type) {
		if (GKT_KEY_OK == minor_type) {
			if (!s_window_initialization_data.combined_key_counter) {
				s_window_initialization_data.add_data.authority = GKT_USER_AUTH_ADMIN;
				s_window_initialization_data.add_data.type = GKT_USER_TYPE_MAX;
				s_window_initialization_data.add_data.user_index = -1;
				gkt_window_exit();
				gkt_window_show(WINDOW_ID_user_add_auth, 
						(uint32_t)&s_window_initialization_data.add_data);
			}
			else {
				gkt_info("window_initialization: combined_key - %u\n", 
					s_window_initialization_data.combined_key_value);

				exit_now = 1;
				switch (s_window_initialization_data.combined_key_value) {
					case WINDOW_OPCODE_COMPATIBLE_SYSTEM_INFO:
						gkt_window_exit();
						gkt_window_show(WINDOW_ID_sysinfo, 0);
						break;
					case WINDOW_OPCODE_COMPATIBLE_FACTORY_INSPECT_1919:	
					case WINDOW_OPCODE_COMPATIBLE_FACTORY_INSPECT_1901:
					case WINDOW_OPCODE_COMPATIBLE_FACTORY_INSPECT_3791:
					case WINDOW_OPCODE_COMPATIBLE_FACTORY_INSPECT_3792:
					case WINDOW_OPCODE_COMPATIBLE_FACTORY_INSPECT_1973:
						gkt_window_exit();
						gkt_window_show(WINDOW_ID_factory_inspect,
							s_window_initialization_data.combined_key_value);
						break;
					case WINDOW_OPCODE_COMPATIBLE_AGING_TEST:
						gkt_window_exit();
						gkt_window_show(WINDOW_ID_aging_test, 0);
						break;
#ifdef GKT_CONFIG_KEYPAD_SENSITIVE_SCAN_STANDBY_ATTR_TABLE							
					case WINDOW_OPCODE_COMPATIBLE_KEYPAD_PARAMETER_STANDARD:
						if ((gp_window_unreset_data->keypad_parameter_value == GKT_KEYPAD_RESPONSE_SENSITIVITY)
							|| (gp_window_unreset_data->keypad_parameter_value == 0)) {
							gp_window_unreset_data->keypad_parameter_value = GKT_KEYPAD_RESPONSE_STANDARD;
							gkt_key_parameter_update(GKT_KEYPAD_RESPONSE_STANDARD);
							window_data_save();
							s_window_initialization_data.set_key_parameter = 1;
							exit_now = 0;
							gkt_window_voice_play(NULL, WINDOW_VOICE_COMBINED_ID(set_success), 
								GKT_WINDOW_VOICE_F_NOTIFY_END | GKT_WINDOW_VOICE_F_NOTIFY_ERROR, NULL);
						}
						break;
					case WINDOW_OPCODE_COMPATIBLE_KEYPAD_PARAMETER_SENSITIVE:
						if (gp_window_unreset_data->keypad_parameter_value != GKT_KEYPAD_RESPONSE_SENSITIVITY) {
							gp_window_unreset_data->keypad_parameter_value = GKT_KEYPAD_RESPONSE_SENSITIVITY;
							gkt_key_parameter_update(GKT_KEYPAD_RESPONSE_SENSITIVITY);
							window_data_save();
							s_window_initialization_data.set_key_parameter = 1;
							exit_now = 0;
							gkt_window_voice_play(NULL, WINDOW_VOICE_COMBINED_ID(set_success), 
								GKT_WINDOW_VOICE_F_NOTIFY_END | GKT_WINDOW_VOICE_F_NOTIFY_ERROR, NULL);
						}
						break;
#endif
					case WINDOW_OPCODE_COMPATIBLE_RFID_STANDBY_SOFT_POWEROFF: 
						if (gp_window_unreset_data->rfid_standby_mode != GKT_RFID_STANBY_SOFT_POWEROFF) {
		
							gp_window_unreset_data->rfid_standby_mode = GKT_RFID_STANBY_SOFT_POWEROFF;/*rfid standby soft poweroff*/
							window_data_save();
							s_window_initialization_data.set_rfid_standby_mode = 1;
							exit_now = 0;
							gkt_window_voice_play(NULL, WINDOW_VOICE_COMBINED_ID(set_success),
								GKT_WINDOW_VOICE_F_NOTIFY_END | GKT_WINDOW_VOICE_F_NOTIFY_ERROR, NULL);
						}
						break;
					case WINDOW_OPCODE_COMPATIBLE_RFID_STANDBY_LPCD:
						if (gp_window_unreset_data->rfid_standby_mode != GKT_RFID_STANBY_NORMAL) {
							
							gp_window_unreset_data->rfid_standby_mode = GKT_RFID_STANBY_NORMAL;/*rfid standby lpcd*/
							window_data_save();
							exit_now = 0;
							s_window_initialization_data.set_rfid_standby_mode = 1;
							gkt_window_voice_play(NULL, WINDOW_VOICE_COMBINED_ID(set_success),
								GKT_WINDOW_VOICE_F_NOTIFY_END | GKT_WINDOW_VOICE_F_NOTIFY_ERROR, NULL);
						}
						break;
					case WINDOW_OPCODE_COMPATIBLE_FAST_STANDBY:
						gkt_window_system_standby(1);
						break;
					case WINDOW_OPCODE_COMPATIBLE_KEYPAD_OPEN_PARAMETER_TEST_PRINT:
						gkt_keypad_parameter_print_open();
						exit_now = 0;
						gkt_window_voice_play(NULL, WINDOW_VOICE_COMBINED_ID(set_success),
								GKT_WINDOW_VOICE_F_NOTIFY_END | GKT_WINDOW_VOICE_F_NOTIFY_ERROR, NULL);
						break;
#if defined(GKT_CONFIG_DOOR_LOCK_ENABLE) && GKT_CONFIG_DOOR_LOCK_ENABLE						
#if defined(GKT_CONFIG_DOOR_LOCK_AUTOMATIC) && GKT_CONFIG_DOOR_LOCK_AUTOMATIC
					case WINDOW_OPCODE_COMPATIBLE_MOTOR_SETTING:
						gkt_window_exit();
						gkt_window_show(WINDOW_ID_motor_setting, 0);
						break;
#else
					case WINDOW_OPCODE_COMPATIBLE_MOTOR_ROTATE_DIRECTION_FORWARD:
						if (gp_window_unreset_data->lock_motor_rotate_direction != GKT_DOOR_LOCK_MOTOR_ROTATE_DIRECTION_FORWARD) {
							gp_window_unreset_data->lock_motor_rotate_direction = GKT_DOOR_LOCK_MOTOR_ROTATE_DIRECTION_FORWARD;
							gkt_door_lock_set_motor_rotate_direction(gp_window_unreset_data->lock_motor_rotate_direction);
							window_data_save();
							exit_now = 0;
							gkt_window_voice_play(NULL, WINDOW_VOICE_COMBINED_ID(set_success),
									GKT_WINDOW_VOICE_F_NOTIFY_END | GKT_WINDOW_VOICE_F_NOTIFY_ERROR, NULL);
						}
						break;
					case WINDOW_OPCODE_COMPATIBLE_MOTOR_ROTATE_DIRECTION_REVERSE:
						if (gp_window_unreset_data->lock_motor_rotate_direction != GKT_DOOR_LOCK_MOTOR_ROTATE_DIRECTION_REVERSE) {
							gp_window_unreset_data->lock_motor_rotate_direction = GKT_DOOR_LOCK_MOTOR_ROTATE_DIRECTION_REVERSE;
							gkt_door_lock_set_motor_rotate_direction(gp_window_unreset_data->lock_motor_rotate_direction);
							window_data_save();
							exit_now = 0;
							gkt_window_voice_play(NULL, WINDOW_VOICE_COMBINED_ID(set_success),
									GKT_WINDOW_VOICE_F_NOTIFY_END | GKT_WINDOW_VOICE_F_NOTIFY_ERROR, NULL);
						}
						break;	
#endif
#endif						
#if defined(GKT_CONFIG_NETWORK_ENABLE) && GKT_CONFIG_NETWORK_ENABLE
					case WINDOW_OPCODE_COMPATIBLE_NETWORK_SETTING:
						gkt_window_exit();
						gkt_window_show(WINDOW_ID_network_setting, 0);
						break;
#endif
					default:
#if defined(GKT_CONFIG_PROXIMITY_SENSOR_ENABLE) && GKT_CONFIG_PROXIMITY_SENSOR_ENABLE
						if ((s_window_initialization_data.combined_key_value >= WINDOW_OPCODE_COMPATIBLE_SET_PR_SENSOR_GEAR_MIN) \
							&& (s_window_initialization_data.combined_key_value <= WINDOW_OPCODE_COMPATIBLE_SET_PR_SENSOR_GEAR_MAX))
						{
							uint8_t pr_sensor_gear = s_window_initialization_data.combined_key_value - WINDOW_OPCODE_COMPATIBLE_SET_PR_SENSOR_GEAR_MIN;
							if (pr_sensor_gear <= GKT_PR_SENSOR_SENSE_GEAR_MAX) {
								gkt_pr_sensor_set_threshold(pr_sensor_gear, 1);	
								exit_now = 0;
								gkt_window_voice_play(NULL, WINDOW_VOICE_COMBINED_ID(operation_success), 0, NULL);
								break;
							}
						}
#endif
						exit_now = 0;
						window_initialization_reset_combined_key();
						gkt_window_voice_play(NULL, 
								WINDOW_VOICE_COMBINED_ID(forbid),
								0, NULL);
						break;
				}

				if (exit_now)
					gkt_window_exit();
			}

			retval = GKT_EVENT_HANDLE_COMPLETE;
		}
		else if ((minor_type >= GKT_KEY_0) && (minor_type <= GKT_KEY_9)) {
			s_window_initialization_data.combined_key_counter++;
			s_window_initialization_data.combined_key_value = 
				(s_window_initialization_data.combined_key_value * 10) + (minor_type - GKT_KEY_0);
			retval = GKT_EVENT_HANDLE_COMPLETE;
		}
		else if ((GKT_KEY_BACKSPACE == minor_type)
			&& (s_window_initialization_data.combined_key_counter > 0)) 
		{
			window_initialization_reset_combined_key();
			retval = GKT_EVENT_HANDLE_COMPLETE;
		}
	}
	else if (GKT_EVENT_VOICE == major_type) {
		if (s_window_initialization_data.set_key_parameter == 1) 
			s_window_initialization_data.set_key_parameter = 0;
		else if (s_window_initialization_data.set_rfid_standby_mode == 1) 
			s_window_initialization_data.set_rfid_standby_mode = 0;
			
		gkt_window_exit();
	}
	
	return retval;
}
GKT_WINDOW_DESC_DEFINE(initialization); 

